package com.gnuc.thoth.app;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.gnuc.thoth.R;
import com.gnuc.thoth.framework.Thoth;

public class PreferencesMain extends PreferenceActivity
{
	Preference syncCapture=null;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Thoth.cx = getBaseContext();
		Thoth.Settings.read();
		addPreferencesFromResource(R.xml.thoth_preferences);
		// Get the custom preference
		syncCapture = (Preference) findPreference("APP_SYNC_CAPTURE");
		syncCapture.setOnPreferenceClickListener(new OnPreferenceClickListener()
		{
			public boolean onPreferenceClick(Preference preference)
			{
				if (!Thoth.Settings.APP_SETUP)
					Thoth.sendToService(Thoth.REQ.SYNC_CAPTURE, null, null);
				Toast.makeText(Thoth.cx, "All logs will be re-scanned. This is a silent process.", Toast.LENGTH_SHORT).show();
				syncCapture.setEnabled(false);
				hdlr.sendEmptyMessageDelayed(0,12000);
				return true;
			}
		});
	}
	
	@Override
	public void onStart()
	{
		super.onStart();
		Thoth.Settings.read();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		Thoth.Settings.APP_ENABLE_GoMAIL = prefs.getBoolean("ENABLE_GoMAIL", Thoth.Settings.APP_ENABLE_GoMAIL);
		Thoth.Settings.APP_ENABLE_GoCAL = prefs.getBoolean("ENABLE_GoCAL", Thoth.Settings.APP_ENABLE_GoCAL);
	}
	
	public Handler	hdlr	= new Handler()
	{
		public void handleMessage(Message msg)
		{
			if(null!=syncCapture)
				syncCapture.setEnabled(false);
		}
	};
	
}
